﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Text;

public partial class MRP_UserPermission : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void UIServer1_CmdFieldFire(object sender, Evolutility.UIServer.CmdFieldEventArgs e)
    {
        //int id = Int32.Parse(e.PK);
        List<int> lstModuleID = new List<int>();
        StringBuilder sbSQL = new StringBuilder();
        if (e.CommandText.Equals("关联到模块"))
        {
            Database db = DatabaseFactory.CreateDatabase("MRPSqlServer");

            //IDataReader reader1 = db.ExecuteReader(CommandType.Text, "Select UserID from EVOL_User where ID=" + Int32.Parse(e.PK));
            using (IDbConnection connection = db.CreateConnection())
            {
                connection.Open();
                string sqlCommand = "Select ID from [EVOL_Module]";

                using (IDataReader reader = db.ExecuteReader(CommandType.Text, sqlCommand))
                {

                    while (reader.Read())
                    {
                        lstModuleID.Add(Int32.Parse(reader["ID"].ToString()));
                    }
                }
                foreach(int mid in lstModuleID)
                {
                    string sqlP = string.Format("Select * from EVOL_UserPermission where UserID={0} and ModuleID={1}", e.PK, mid);
                    using (IDataReader reader = db.ExecuteReader(CommandType.Text, sqlP))
                    {
                        if (!reader.Read())
                        {
                            sbSQL.AppendFormat("Insert Into EVOL_UserPermission([UserID],[ModuleID]) values({0},{1});", e.PK, mid);

                        }
                    }
                }
                if(sbSQL.Length>20)
                    db.ExecuteNonQuery(CommandType.Text, sbSQL.ToString());
                
                connection.Close();
            }
            
        }
    }
}
